// import request from '@/utils/request';
// 🦜 : let's add our mocks here...
// const tokens = {admin: {token: 'admin-token'}, editor: {token: 'editor-token'}};
// const users = {
//     'admin-token': {roles: ['admin'], introduction: 'I am a super administrator', avatar: a, name: 'Test User AAA'},
//     'editor-token': {roles: ['editor'], introduction: 'I am an editor', avatar: a, name: 'Normal Editor'}
// };

// const tokens = {admin: {token: 'admin-token'}, editor: {token: 'editor-token'}};
// const users = {
//     'admin-token': {roles: ['admin'], introduction: 'I am a super administrator',
//                     // avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
//                     avatar: '/black.jpeg',
//                     name: 'Super Admin'},
//     'editor-token': {
//         roles: ['editor'], introduction: 'I am an editor',
//         avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', name: 'Normal Editor'}
// };

// --------------------------------------------------
import U1 from '@/c04-user.json';
const users = {};               // token -> obj
const tokens = {};              // username,pswd -> token

function makeToken(t) {return {token: t, password: '123456'};}
function makeUser(name, intro){
    const a = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif';
    return {roles: ['admin'], introduction: intro, avatar: a, name:name};
}
// 🦜 : for now: let username = name
for (var i = 0; i < 4; i++) {
    const t = `token-${i}`; const name = `TestUser${i}`;
    tokens[name] = makeToken(t);
    users[t] = makeUser(`The test user ${i}`,name);
    // users[t] = {roles: ['admin'], introduction: `The test user ${i}`, avatar: a , name: name};
}
for (var i = 0; i < U1.length; i++) {
    let j = i + 4; // start from 4 to avoid conflicts with the above
    const name = U1[i];
    const t = `token-${name}`;
    tokens[name] = makeToken(t);
    users[t] = makeUser(`The test user ${j}`,name);
}

// 🦜 : add caesar
let tCaesar = 'Caesar';
let nCaesar = 'Caesar';
tokens[nCaesar] = makeToken(tCaesar);
users[tCaesar] = makeUser(`A stabbable admin user`,nCaesar);

console.log('⚙️ tokens', tokens);
console.log('⚙️ users', users);

function PromiseU<T>(o: T): Promise<T> {
    return new Promise((resolve, reject) => {
        resolve(o);
    });
}

export function login(data) {
    // let o = request({url: '/dirty/user/login', method: 'post', data});
    console.log('login with', data);
    console.log('tokens', tokens);
    console.log('users', users);
    const {username, password} = data;
    const token = tokens[username];
    let o = new Promise((Y, N) => {
        setTimeout(() => {
            if (!token || password !== token.password) {
                Y({code: 1, message: 'Account and password are incorrect.'});
            }
            let r = {code: 0, data: token};
            Y(r);
        }, 500);
    });
    console.log('login with', o);
    return o;
}


export function getInfo(token) {
    console.log('gi: tokens', tokens);
    console.log('gi: token', token);

    console.log('gi: users', users);
    // return request({url: '/dirty/user/info', method: 'get', params: { token }});
    const info = users[token];
    if (!info)
        return PromiseU({code: 1, message: 'Login failed, unable to get user details.'});

    let o = new Promise((Y, N) => {
        setTimeout(() => {
            let r = {code: 0, data: info};
            Y(r);
        }, 500);
    });
    return o;
}

export function logout(token) {
  // return request({url: '/dirty/user/logout', method: 'post', params: { token }});
    return new Promise((Y, N) => {Y({code: 0, data: 'success'});});
}
